package cn.ibizlab.modeling.groovy;

import groovy.transform.CompileStatic
import net.ibizsys.central.dataentity.action.DEActionRuntimeBase
import net.ibizsys.central.util.IEntityDTO
import net.ibizsys.runtime.util.KeyValueUtils
import org.eclipse.jgit.api.*
import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider

import java.nio.charset.StandardCharsets
import java.nio.file.*
import net.ibizsys.central.dataentity.IDataEntityRuntime

import java.util.stream.Stream

@CompileStatic
class GitRegisterDEActionRuntime extends DEActionRuntimeBase{

    public GitRegisterDEActionRuntime() {
    }

    @Override
    protected Object onExecute(IEntityDTO iEntityDTO) throws Throwable {
        String psdevcentersvnid = iEntityDTO.getString("psdevcentersvnid",KeyValueUtils.genGuidEx())
        iEntityDTO.set("psdevcentersvnid",psdevcentersvnid)
        String pssvninstrepoid = iEntityDTO.getString("sourcepssvninstrepoid",iEntityDTO.getString("pssvninstrepoid",psdevcentersvnid))

        if(pssvninstrepoid && !pssvninstrepoid.equals(psdevcentersvnid)) {
            IEntityDTO repo = null
            try {

                IDataEntityRuntime entityDataEntityRuntime = this.getSystemRuntime().getDataEntityRuntime("PSSVNINSTREPO")
                repo = entityDataEntityRuntime.get(pssvninstrepoid,true)
                if(!repo) {
                    throw new Exception("云平台仓库参数缺失");
                }
                repo.set("pssvninstrepoid",psdevcentersvnid)
                repo.set("pssvninstreponame",iEntityDTO.get("psdevcentersvnname"))
                repo.set("gitpath",iEntityDTO.get("gitpath"))
                repo.set("connstr",iEntityDTO.get("gitpath"))
                repo.set("gitbranch",iEntityDTO.get("gitbranch"))

                

                entityDataEntityRuntime.create(repo)
                iEntityDTO.set("pssvninstrepoid",psdevcentersvnid)
                iEntityDTO.set("pssvninstreponame",iEntityDTO.get("psdevcentersvnname"))

                this.getDataEntityRuntime().create(iEntityDTO);
                repo.set("psdevcenterid",iEntityDTO.get("psdevcenterid"))
                repo.set("psdevcentername",iEntityDTO.get("psdevcentername"))
                repo.set("svntype",iEntityDTO.get("svntype"))
                repo.set("resstate",iEntityDTO.get("repostate"))
                entityDataEntityRuntime.update(repo)

            }
            catch (Exception ex) {

            }

        }
        else {
            this.getDataEntityRuntime().create(iEntityDTO);
        }
        return iEntityDTO
    }


}